﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using TTQT.TTQT_Code;

/// <summary>
/// Summary description for SanPham
/// </summary>
public class SanPhamDAO
{
    public string id = "";
    public string seri = "";
    public string nhomId = "";
    public string parentId = "";
    public string loaiId = "";
    public string tenHang = "";
    public string tomLuoc = "";
    public string noiDung = "";
    private string giaBanThamKhao = "0";
    public string picUrl = "";
    public string userName = "";
    public string ngayNhap = "";
    public string active = "";
    public string del = "";
    public string hangMoi = "";
    public string daBan = "";
    public string spHot = "";
    public string banChay = "";
    public string conHang = "";
    public string thongSo = "";
    public string quaTang = "";
    private string chietKhau = "0";
    private string giaBanCoChietKhau = "0";

    private string tenNhom = "";
    private string noiSXID = "";
    private string tenNoiSX = "";
    private string tenLoai = "";


    public int soLuongDatMua = 0;//Dung cho gio hang

    public string Id
    {
        get { return id; }
    }

    public string GiaBanCoChietKhau
    {
        get { return giaBanCoChietKhau; }
        set
        {
            giaBanCoChietKhau = value;
            if (string.IsNullOrEmpty(value))
                giaBanCoChietKhau = "0";
        }
    }

    public string ChietKhau
    {
        get { return chietKhau; }
        set
        {

            chietKhau = value;
            if (string.IsNullOrEmpty(value))
                chietKhau = "0";
        }
    }


    public string Seri
    {
        get { return seri; }
    }

    public string NhomId
    {
        get { return nhomId; }
    }

    public string ParentId
    {
        get { return parentId; }
    }

    public string LoaiId
    {
        get { return loaiId; }
    }

    public string TenHang
    {
        get { return tenHang; }
    }

    public string TomLuoc
    {
        get { return tomLuoc; }
    }

    public string NoiDung
    {
        get { return noiDung; }
    }

    public string GiaBanThamKhao
    {
        get { return giaBanThamKhao; }
        set
        {
            giaBanThamKhao = value;
            if (string.IsNullOrEmpty(value))
                giaBanThamKhao = "0";
        }
    }

    public string PicUrl
    {
        get { return picUrl; }
    }

    public string UserName
    {
        get { return userName; }
    }

    public string NgayNhap
    {
        get { return ngayNhap; }
    }

    public string Active
    {
        get { return active; }
    }

    public string Del
    {
        get { return del; }
    }

    public string HangMoi
    {
        get { return hangMoi; }
    }

    public string DaBan
    {
        get { return daBan; }
    }
    public string NoiSxid
    {
        get { return noiSXID; }
        set { noiSXID = value; }
    }

    public int SoLuongDatMua
    {
        get { return soLuongDatMua; }
    }
    public string sanPHam_GetTopCungNhom_Json(string id, string soluong)
    {
        string kq = "";
        DataTable tb = sanPHam_GetTopCungNhom(id, soluong);
        ArrayList infos = new ArrayList();
        foreach (DataRow r in tb.Rows)
        {
            SanPhamInfo info = new SanPhamInfo();
            info.Id = r["id"].ToString();
            info.Seri = r["Seri"].ToString();
            info.NgayNhap = r["NgayNhap"].ToString();
            //info.MaCN = r["MaCN"].ToString();
            info.TenHang = r["TenHang"].ToString();
            info.PicUrl = r["PicUrl"].ToString();
            //info.GhiChu = r["GhiChu"].ToString();
            //info.MaVach = r["MaVach"].ToString();
            //info.GiaNhap = r["GiaNhap"].ToString();
            info.GiaBanThamKhao = r["GiaBanThamKhao"].ToString();
            info.ChietKhau = r["ChietKhau"].ToString();
            info.GiaBanCoChietKhau = r["GiaBanCoChietKhau"].ToString();
            //info.GiaBanThucTe = r["GiaBanThucTe"].ToString();
            //info.SoLuong = r["SoLuong"].ToString();
            info.UserName = r["UserName"].ToString();
            info.Del = r["Del"].ToString();
            info.HangMoi = r["HangMoi"].ToString();
            info.DaBan = r["DaBan"].ToString();
            info.Active = r["active"].ToString();
            //info.NoiDung = r["NoiDung"].ToString();
            //info.TomLuoc = r["TomLuoc"].ToString();
            info.LoaiId = r["LoaiId"].ToString();
            info.NhomId = r["NhomId"].ToString();
            info.ParentId = r["parentId"].ToString();
            info.SpHot = r["spHot"].ToString();
            info.BanChay = r["banChay"].ToString();

            info.TenNhom = r["tenNhom"].ToString();
            info.NoiSxid = r["noiSXID"].ToString();
            info.TenNoiSx = r["tenNoiSX"].ToString();
            info.TenLoai = r["tenLoai"].ToString();

            infos.Add(info);
        }
        kq = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(infos);
        return kq;//kkkkkkkkkkkkkkkkkkkllllllllllllllllll
    }
    public string sanPHam_GetByNhomId_Json(string nhomId, string active)
    {
        string kq = "";
        DataTable tb = sanPHam_GetByNhomId(nhomId, active);
        ArrayList infos = new ArrayList();
        foreach (DataRow r in tb.Rows)
        {
            SanPhamInfo info = new SanPhamInfo();
            info.Id = r["id"].ToString();
            info.Seri = r["Seri"].ToString();
            info.NgayNhap = r["NgayNhap"].ToString();
            //info.MaCN = r["MaCN"].ToString();
            info.TenHang = r["TenHang"].ToString();
            info.PicUrl = r["PicUrl"].ToString();
            //info.GhiChu = r["GhiChu"].ToString();
            //info.MaVach = r["MaVach"].ToString();
            //info.GiaNhap = r["GiaNhap"].ToString();
            info.GiaBanThamKhao = r["GiaBanThamKhao"].ToString();
            info.ChietKhau = r["ChietKhau"].ToString();
            info.GiaBanCoChietKhau = r["GiaBanCoChietKhau"].ToString();
            //info.GiaBanThucTe = r["GiaBanThucTe"].ToString();
            //info.SoLuong = r["SoLuong"].ToString();
            info.UserName = r["UserName"].ToString();
            info.Del = r["Del"].ToString();
            info.HangMoi = r["HangMoi"].ToString();
            info.DaBan = r["DaBan"].ToString();
            info.Active = r["active"].ToString();
            info.NoiDung = r["NoiDung"].ToString();
            info.TomLuoc = r["TomLuoc"].ToString();
            info.LoaiId = r["LoaiId"].ToString();
            info.NhomId = r["NhomId"].ToString();
            info.ParentId = r["parentId"].ToString();
            info.SpHot = r["spHot"].ToString();
            info.BanChay = r["banChay"].ToString();

            info.TenNhom = r["tenNhom"].ToString();
            info.NoiSxid = r["noiSXID"].ToString();
            info.TenNoiSx = r["tenNoiSX"].ToString();
            info.TenLoai = r["tenLoai"].ToString();
            info.QuaTang = r["QuaTang"].ToString();
            info.ConHang = r["ConHang"].ToString();
            infos.Add(info);
        }
        kq = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(infos);
        return kq;//kkkkkkkkkkkkkkkkkkkllllllllllllllllll
    }
    //Cach nay eo' nhanh bang su dung sqlDataAdapter
    public DataTable GetDataTableFromDataReader1(IDataReader dataReader)
    {
        DataTable schemaTable = dataReader.GetSchemaTable();
        DataTable resultTable = new DataTable();

        foreach (DataRow dataRow in schemaTable.Rows)
        {
            DataColumn dataColumn = new DataColumn();
            dataColumn.ColumnName = dataRow["ColumnName"].ToString();
            dataColumn.DataType = Type.GetType(dataRow["DataType"].ToString());
            dataColumn.ReadOnly = (bool)dataRow["IsReadOnly"];
            dataColumn.AutoIncrement = (bool)dataRow["IsAutoIncrement"];
            dataColumn.Unique = (bool)dataRow["IsUnique"];

            resultTable.Columns.Add(dataColumn);
        }

        while (dataReader.Read())
        {
            DataRow dataRow = resultTable.NewRow();
            for (int i = 0; i < resultTable.Columns.Count - 1; i++)
            {
                dataRow[i] = dataReader[i];
            }
            resultTable.Rows.Add(dataRow);
        }

        return resultTable;
    }
    //public DataTable GetDataTableFast(string query)
    //{
    //    connection.Open();
    //    DateTime start = DateTime.Now;
    //    IDataReader rdr = new SqlCommand(query, connection).ExecuteReader();
    //    DataTable resultTable = GetDataTableFromDataReader(rdr);
    //    TimeSpan ts = DateTime.Now.Subtract(start);
    //    System.Diagnostics.Trace.Write("Time Elapsed in Custom : " + ts.TotalMilliseconds);
    //    connection.Close();
    //    return resultTable;
    //}
    public int insert()
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("SanPham_Insert", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@seri", seri);
        cmd.Parameters.AddWithValue("@parentId", parentId);
        cmd.Parameters.AddWithValue("@nhomId", nhomId);
        cmd.Parameters.AddWithValue("@loaiId", loaiId);
        cmd.Parameters.AddWithValue("@tenHang", tenHang);
        cmd.Parameters.AddWithValue("@tomLuoc", tomLuoc);
        cmd.Parameters.AddWithValue("@noiDung", noiDung);
        cmd.Parameters.AddWithValue("@giaBanThamkhao", giaBanThamKhao);
        cmd.Parameters.AddWithValue("@picUrl", picUrl);
        cmd.Parameters.AddWithValue("@userName", userName);
        cmd.Parameters.AddWithValue("@hangMoi", hangMoi);
        cmd.Parameters.AddWithValue("@spHot", spHot);
        cmd.Parameters.AddWithValue("@banChay", banChay);
        cmd.Parameters.AddWithValue("@conHang", conHang);
        cmd.Parameters.AddWithValue("@thongSo", thongSo);
        cmd.Parameters.AddWithValue("@quaTang", quaTang);
        cmd.Parameters.AddWithValue("@chietKhau", chietKhau);
        cmd.Parameters.AddWithValue("@NoiSXID", noiSXID);
        cmd.Parameters.AddWithValue("@GiaBanCoChietKhau", giaBanCoChietKhau);
        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteScalar(), 0);
            id = kq.ToString();
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    public bool getById()
    {
        bool kq = false;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("SanPham_GetById", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@id", id);
        try
        {
            conn.Open();
            SqlDataReader dr = comm.ExecuteReader();
            if (dr.Read())
            {
                kq = true;
                seri = TTQTCommon.ToString(dr["seri"]);
                ngayNhap = TTQTCommon.ToString(dr["ngayNhap"]);
                nhomId = TTQTCommon.ToString(dr["NhomId"]);
                picUrl = TTQTCommon.ToString(dr["PicUrl"]);
                loaiId = TTQTCommon.ToString(dr["LoaiId"]);
                tenHang = TTQTCommon.ToString(dr["TenHang"]);
                tomLuoc = TTQTCommon.ToString(dr["TomLuoc"]);
                noiDung = TTQTCommon.ToString(dr["NoiDung"]);
                giaBanThamKhao = TTQTCommon.ToString(dr["GiaBanThamKhao"]);
                userName = TTQTCommon.ToString(dr["UserName"]);
                del = TTQTCommon.ToString(dr["del"]);
                hangMoi = TTQTCommon.ToString(dr["hangMoi"]);
                daBan = TTQTCommon.ToString(dr["daBan"]);
                parentId = TTQTCommon.ToString(dr["parentId"]);

                hangMoi = TTQTCommon.ToString(dr["hangMoi"]);
                spHot = TTQTCommon.ToString(dr["spHot"]);
                banChay = TTQTCommon.ToString(dr["banChay"]);
                conHang = TTQTCommon.ToString(dr["conHang"]);
                thongSo = TTQTCommon.ToString(dr["thongSo"]);
                quaTang = TTQTCommon.ToString(dr["quaTang"]);
                

                chietKhau = TTQTCommon.ToString(dr["chietKhau"]);
                noiSXID = TTQTCommon.ToString(dr["NoiSXID"]);
                giaBanCoChietKhau = TTQTCommon.ToString(dr["GiaBanCoChietKhau"]);

            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return kq;
    }
    public string sanPhamInfo_getById_json(string sid)
    {
        string kq = "";
        ArrayList infos = new ArrayList();
        SanPhamInfo info = getById(sid);
        if(info!=null)
            infos.Add(info);
        kq = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(infos);
        return kq;//kkkkkkkkkkkkkkkkkkkllllllllllllllllll
    }
    public SanPhamInfo getById(string id)
    {
        SanPhamInfo kq = null;// new SanPhamInfo();
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("SanPhamInfo_GetById", conn);
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@id", id);
        try
        {
            conn.Open();
            SqlDataReader dr = comm.ExecuteReader();
            if (dr.Read())
            {
                kq = new SanPhamInfo();
                kq.Id = TTQTCommon.ToString(dr["Id"]);
                kq.Seri = TTQTCommon.ToString(dr["seri"]);
                kq.NgayNhap = TTQTCommon.ToString(dr["ngayNhap"]);
                kq.NhomId = TTQTCommon.ToString(dr["NhomId"]);
                kq.PicUrl = TTQTCommon.ToString(dr["PicUrl"]);
                kq.LoaiId = TTQTCommon.ToString(dr["LoaiId"]);
                kq.TenHang = TTQTCommon.ToString(dr["TenHang"]);
                kq.TomLuoc = TTQTCommon.ToString(dr["TomLuoc"]);
                kq.NoiDung =TTQTCommon.ToString(dr["NoiDung"]);
                kq.GiaBanThamKhao = TTQTCommon.ToString(dr["GiaBanThamKhao"]);
                kq.UserName = TTQTCommon.ToString(dr["UserName"]);
                kq.Del = TTQTCommon.ToString(dr["del"]);
                kq.HangMoi = TTQTCommon.ToString(dr["hangMoi"]);
                kq.DaBan = TTQTCommon.ToString(dr["daBan"]);
                kq.ParentId = TTQTCommon.ToString(dr["parentId"]);

                kq.HangMoi = TTQTCommon.ToString(dr["hangMoi"]);
                kq.SpHot = TTQTCommon.ToString(dr["spHot"]);
                kq.BanChay = TTQTCommon.ToString(dr["banChay"]);
                kq.ConHang = TTQTCommon.ToString(dr["ConHang"]);

                //kq.thongSo = TTQTCommon.ToString(dr["thongSo"]);
                kq.QuaTang = TTQTCommon.ToString(dr["quaTang"]);


                kq.ChietKhau = TTQTCommon.ToString(dr["chietKhau"]);
                kq.NoiSxid = TTQTCommon.ToString(dr["NoiSXID"]);
                kq.GiaBanCoChietKhau = TTQTCommon.ToString(dr["GiaBanCoChietKhau"]);

                kq.TenNhom = TTQTCommon.ToString(dr["tenNhom"]); 
                kq.TenNoiSx = TTQTCommon.ToString(dr["tenNoiSX"]);
                kq.TenLoai = TTQTCommon.ToString(dr["tenLoai"]); 
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return kq;
    }
    public int update()
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("SanPham_Update", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", id);
        cmd.Parameters.AddWithValue("@seri", seri);
        cmd.Parameters.AddWithValue("@parentId", parentId);
        cmd.Parameters.AddWithValue("@nhomId", nhomId);
        cmd.Parameters.AddWithValue("@loaiId", loaiId);
        cmd.Parameters.AddWithValue("@tenHang", tenHang);
        cmd.Parameters.AddWithValue("@tomLuoc", tomLuoc);
        cmd.Parameters.AddWithValue("@noiDung", noiDung);
        cmd.Parameters.AddWithValue("@giaBanThamkhao", giaBanThamKhao);
        cmd.Parameters.AddWithValue("@picUrl", picUrl);
        cmd.Parameters.AddWithValue("@userName", userName);
        cmd.Parameters.AddWithValue("@hangMoi", hangMoi);
        cmd.Parameters.AddWithValue("@spHot", spHot);
        cmd.Parameters.AddWithValue("@banChay", banChay);
        cmd.Parameters.AddWithValue("@conHang", conHang);
        cmd.Parameters.AddWithValue("@thongSo", thongSo);
        cmd.Parameters.AddWithValue("@quaTang", quaTang);
        cmd.Parameters.AddWithValue("@chietKhau", chietKhau);
        cmd.Parameters.AddWithValue("@NoiSXID", noiSXID);
        cmd.Parameters.AddWithValue("@giaBanCoChietKhau", giaBanCoChietKhau);
        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteNonQuery(), 0);
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    public int delete()
    {
        int kq = 0;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand cmd = new SqlCommand("SanPham_Delete", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", id);
        try
        {
            conn.Open();
            kq = TTQTCommon.ToInt32(cmd.ExecuteNonQuery(), 0);
        }
        catch (Exception exxx)
        { }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
        return kq;
    }
    public DataTable timKiem(string keyWord, string nhomId, string loaiId, string giaTien)
    {
        if (keyWord.Equals("Nhập từ khóa"))
        {
            keyWord = "";
        }
        DataTable result = null;
        string sql =
            "select SanPham.id,NhomId,LoaiId,seri,Tenhang,SanPham_Nhom.[Name] as TenNhom,TomLuoc,NoiDung,GiaBanThamKhao,PicUrl,userName,convert(varchar(10),NgayNhap,103) as ngayNhap,hangmoi,active,del,isnull(hangmoi,0) as hangmoi,isnull(sphot,0) as sphot,isnull(banchay,0) as banchay,isnull(conHang,0) as conHang,quaTang from SanPham left join SanPham_Nhom on sanpham.NhomId=SanPham_nhom.id";
        string whereClause = " where 1=1";
        if (!string.IsNullOrEmpty(keyWord))
        {
            whereClause += " and (tenHang like N'%" + keyWord + "%' or tomLuoc like '%" + keyWord + "%' or noiDung like '%" + keyWord + "%')";
        }
        whereClause += " and (nhomId=" + nhomId + " or -1=" + nhomId + ")";
        whereClause += " and (loaiId=" + loaiId + " or -1=" + loaiId + ")";
        if (!giaTien.Equals("-1"))
        {
            whereClause += " and GiaBanThamKhao " + giaTien;
        }
        sql += whereClause;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand(sql, conn);

        comm.CommandType = CommandType.Text;
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;

    }
    public string sanPHam_GetTopCoGiamGia_Json(string soluong)
    {
        string kq = "";
        DataTable tb = sanPHam_GetTopCoGiamGia(soluong);
        ArrayList infos = new ArrayList();
        foreach (DataRow r in tb.Rows)
        {
            SanPhamInfo info = new SanPhamInfo();
            info.Id = r["id"].ToString();
            info.Seri = r["Seri"].ToString();
            info.NgayNhap = r["NgayNhap"].ToString();
            //info.MaCN = r["MaCN"].ToString();
            info.TenHang = r["TenHang"].ToString();
            info.PicUrl = r["PicUrl"].ToString();
            //info.GhiChu = r["GhiChu"].ToString();
            //info.MaVach = r["MaVach"].ToString();
            //info.GiaNhap = r["GiaNhap"].ToString();
            info.GiaBanThamKhao = r["GiaBanThamKhao"].ToString();
            info.ChietKhau = r["ChietKhau"].ToString();
            info.GiaBanCoChietKhau = r["GiaBanCoChietKhau"].ToString();
            //info.GiaBanThucTe = r["GiaBanThucTe"].ToString();
            //info.SoLuong = r["SoLuong"].ToString();
            info.UserName = r["UserName"].ToString();
            info.Del = r["Del"].ToString();
            info.HangMoi = r["HangMoi"].ToString();
            info.DaBan = r["DaBan"].ToString();
            info.Active = r["active"].ToString();
            info.NoiDung = r["NoiDung"].ToString();
            info.TomLuoc = r["TomLuoc"].ToString();
            info.LoaiId = r["LoaiId"].ToString();
            info.NhomId = r["NhomId"].ToString();
            info.ParentId = r["parentId"].ToString();
            info.SpHot = r["spHot"].ToString();
            info.BanChay = r["banChay"].ToString();
            info.ConHang = r["ConHang"].ToString();
            info.QuaTang = r["QuaTang"].ToString();

            info.TenNhom = r["tenNhom"].ToString();
            info.NoiSxid = r["noiSXID"].ToString();
            info.TenNoiSx = r["tenNoiSX"].ToString();
            info.TenLoai = r["tenLoai"].ToString();

            infos.Add(info);
        }
        kq = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(infos);
        return kq;//kkkkkkkkkkkkkkkkkkkllllllllllllllllll
    }
    public DataTable sanPHam_GetTopCoGiamGia(string soluong)
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("sanPHam_GetTopCoGiamGia", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.AddWithValue("@soluong", soluong);
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    public string sanPHam_GetTopHot_Json(string soluong)
    {
        string kq = "";
        DataTable tb = sanPHam_GetTopHot(soluong);
        ArrayList infos = new ArrayList();
        foreach (DataRow r in tb.Rows)
        {
            SanPhamInfo info = new SanPhamInfo();
            info.Id = r["id"].ToString();
            info.Seri = r["Seri"].ToString();
            info.NgayNhap = r["NgayNhap"].ToString();
            //info.MaCN = r["MaCN"].ToString();
            info.TenHang = r["TenHang"].ToString();
            info.PicUrl = r["PicUrl"].ToString();
            //info.GhiChu = r["GhiChu"].ToString();
            //info.MaVach = r["MaVach"].ToString();
            //info.GiaNhap = r["GiaNhap"].ToString();
            info.GiaBanThamKhao = r["GiaBanThamKhao"].ToString();
            info.ChietKhau = r["ChietKhau"].ToString();
            info.GiaBanCoChietKhau = r["GiaBanCoChietKhau"].ToString();
            //info.GiaBanThucTe = r["GiaBanThucTe"].ToString();
            //info.SoLuong = r["SoLuong"].ToString();
            info.UserName = r["UserName"].ToString();
            info.Del = r["Del"].ToString();
            info.HangMoi = r["HangMoi"].ToString();
            info.DaBan = r["DaBan"].ToString();
            info.Active = r["active"].ToString();
            //info.NoiDung = r["NoiDung"].ToString();
            //info.TomLuoc = r["TomLuoc"].ToString();
            info.LoaiId = r["LoaiId"].ToString();
            info.NhomId = r["NhomId"].ToString();
            info.ParentId = r["parentId"].ToString();
            info.SpHot = r["spHot"].ToString();
            info.BanChay = r["banChay"].ToString();
            info.ConHang = r["ConHang"].ToString();
            info.QuaTang = r["QuaTang"].ToString();

            info.TenNhom = r["tenNhom"].ToString();
            info.NoiSxid = r["noiSXID"].ToString();
            info.TenNoiSx = r["tenNoiSX"].ToString();
            info.TenLoai = r["tenLoai"].ToString();

            infos.Add(info);
        }
        kq = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(infos);
        return kq;//kkkkkkkkkkkkkkkkkkkllllllllllllllllll
    }
    public DataTable sanPHam_GetTopHot(string soluong)
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("sanPHam_GetTopHot", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.AddWithValue("@soluong", soluong);
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    public string sanPHam_GetTopBanChay_Json(string soluong)
    {
        string kq = "";
        DataTable tb = sanPHam_GetTopBanChay(soluong);
        ArrayList infos = new ArrayList();
        foreach (DataRow r in tb.Rows)
        {
            SanPhamInfo info = new SanPhamInfo();
            info.Id = r["id"].ToString();
            info.Seri = r["Seri"].ToString();
            info.NgayNhap = r["NgayNhap"].ToString();
            //info.MaCN = r["MaCN"].ToString();
            info.TenHang = r["TenHang"].ToString();
            info.PicUrl = r["PicUrl"].ToString();
            //info.GhiChu = r["GhiChu"].ToString();
            //info.MaVach = r["MaVach"].ToString();
            //info.GiaNhap = r["GiaNhap"].ToString();
            info.GiaBanThamKhao = r["GiaBanThamKhao"].ToString();
            info.ChietKhau = r["ChietKhau"].ToString();
            info.GiaBanCoChietKhau = r["GiaBanCoChietKhau"].ToString();
            //info.GiaBanThucTe = r["GiaBanThucTe"].ToString();
            //info.SoLuong = r["SoLuong"].ToString();
            info.UserName = r["UserName"].ToString();
            info.Del = r["Del"].ToString();
            info.HangMoi = r["HangMoi"].ToString();
            info.DaBan = r["DaBan"].ToString();
            info.Active = r["active"].ToString();
            //info.NoiDung = r["NoiDung"].ToString();
            //info.TomLuoc = r["TomLuoc"].ToString();
            info.LoaiId = r["LoaiId"].ToString();
            info.NhomId = r["NhomId"].ToString();
            info.ParentId = r["parentId"].ToString();
            info.SpHot = r["spHot"].ToString();
            info.BanChay = r["banChay"].ToString();
            info.ConHang = r["ConHang"].ToString();
            info.QuaTang = r["QuaTang"].ToString();

            info.TenNhom = r["tenNhom"].ToString();
            info.NoiSxid = r["noiSXID"].ToString();
            info.TenNoiSx = r["tenNoiSX"].ToString();
            info.TenLoai = r["tenLoai"].ToString();

            infos.Add(info);
        }
        kq = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(infos);
        return kq;//kkkkkkkkkkkkkkkkkkkllllllllllllllllll
    }
    public DataTable sanPHam_GetTopBanChay(string soluong)
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("sanPHam_GetTopBanChay", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.AddWithValue("@soluong", soluong);
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    public DataTable sanPHam_GetTopCungNhom(string id, string soluong)
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("sanPHam_GetTopCungNhom", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.AddWithValue("@id", id);
        comm.Parameters.AddWithValue("@soluong", soluong);
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    public DataTable sanPHam_GetByNhomId(string nhomId, string iActive)
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("sanPHam_GetByNhomId", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.AddWithValue("@NhomId", nhomId);
        comm.Parameters.AddWithValue("@iActive", iActive);
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    public PagingOpbject sanPham_GetByNhomId_Paging(string nhomId, string iActive, int pageSize, int currentPage)
    {
        PagingOpbject kq = new PagingOpbject();
        DataTable result = null;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("sanPHam_GetByNhomId_Paging", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.Add("@NhomId", SqlDbType.Int, 4).Value = nhomId;
        comm.Parameters.Add("@iActive", SqlDbType.Int, 4).Value = iActive;

        comm.Parameters.Add("@PageSize", SqlDbType.Int, 4).Value = pageSize;
        comm.Parameters.Add("@CurrentPage", SqlDbType.Int, 4).Value = currentPage;
        comm.Parameters.Add("@ItemCount", SqlDbType.Int).Direction = ParameterDirection.Output;
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
            kq.tableReturn = result;
            kq.totalRecords = Convert.ToInt32(comm.Parameters["@ItemCount"].Value);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return kq;
    }
    public bool isSanPham(SanPhamDAO sp)
    {
        if (this.id == sp.id)
        {
            return true;
        }
        return false;
    }
    public DataTable SanPham_GetSanPhamMoi()
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("SanPham_GetSanPhamMoi", conn);
        comm.CommandType = CommandType.StoredProcedure;
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    /// <summary>
    /// Dung cho module get top 3 san pham hot
    /// </summary>
    /// <returns></returns>
    public DataTable SanPham_GetSanPhamHot()
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("SanPham_GetSanPhamHot", conn);
        comm.CommandType = CommandType.StoredProcedure;
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    /// <summary>
    /// Dung cho module select top 3 sp ban chay
    /// </summary>
    /// <returns></returns>
    public DataTable SanPham_GetSanPhamBanChay()
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("SanPham_GetSanPhamBanChay", conn);
        comm.CommandType = CommandType.StoredProcedure;
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    /// <summary>
    /// Lay 10 san pham cung loai
    /// </summary>
    /// <returns></returns>
    public DataTable SanPham_GetRelated(string idSanPham)
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("SanPham_GetRelated", conn);
        comm.Parameters.Clear();
        comm.Parameters.Add("@ID", SqlDbType.Int, 4).Value = idSanPham;
        comm.CommandType = CommandType.StoredProcedure;
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    public string sanPham_TimKiem_Json(string keyword)
    {
        string kq = "";
        DataTable tb = sanPham_TimKiem(keyword);
        ArrayList infos = new ArrayList();
        foreach (DataRow r in tb.Rows)
        {
            SanPhamInfo info = new SanPhamInfo();
            info.Id = r["id"].ToString();
            info.Seri = r["Seri"].ToString();
            info.NgayNhap = r["NgayNhap"].ToString();
            //info.MaCN = r["MaCN"].ToString();
            info.TenHang = r["TenHang"].ToString();
            info.PicUrl = r["PicUrl"].ToString();
            //info.GhiChu = r["GhiChu"].ToString();
            //info.MaVach = r["MaVach"].ToString();
            //info.GiaNhap = r["GiaNhap"].ToString();
            info.GiaBanThamKhao = r["GiaBanThamKhao"].ToString();
            info.ChietKhau = r["ChietKhau"].ToString();
            info.GiaBanCoChietKhau = r["GiaBanCoChietKhau"].ToString();
            //info.GiaBanThucTe = r["GiaBanThucTe"].ToString();
            //info.SoLuong = r["SoLuong"].ToString();
            info.UserName = r["UserName"].ToString();
            info.Del = r["Del"].ToString();
            info.HangMoi = r["HangMoi"].ToString();
            info.DaBan = r["DaBan"].ToString();
            info.Active = r["active"].ToString();
            info.NoiDung = r["NoiDung"].ToString();
            info.TomLuoc = r["TomLuoc"].ToString();
            info.LoaiId = r["LoaiId"].ToString();
            info.NhomId = r["NhomId"].ToString();
            info.ParentId = r["parentId"].ToString();
            info.SpHot = r["spHot"].ToString();
            info.BanChay = r["banChay"].ToString();
            info.ConHang = r["ConHang"].ToString();
            info.QuaTang = r["QuaTang"].ToString();

            info.TenNhom = r["tenNhom"].ToString();
            info.NoiSxid = r["noiSXID"].ToString();
            info.TenNoiSx = r["tenNoiSX"].ToString();
            info.TenLoai = r["tenLoai"].ToString();

            infos.Add(info);
        }
        kq = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(infos);
        return kq;//kkkkkkkkkkkkkkkkkkkllllllllllllllllll
    }
    public DataTable sanPham_TimKiem(string keyword)
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("sanPham_TimKiem", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.AddWithValue("@keyword", keyword);
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
    public string sanPham_autoCompleteName_json(string keyword)
    {
        string kq = "";
        DataTable tb = sanPham_autoCompleteName(keyword);
        ArrayList infos = new ArrayList();
        foreach (DataRow r in tb.Rows)
        {
            infos.Add(r["tenHang"].ToString());
        }
        kq = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(infos);
        return kq;//kkkkkkkkkkkkkkkkkkkllllllllllllllllll
    }
    public DataTable sanPham_autoCompleteName(string keyword)
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("sanPham_autoCompleteName", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.AddWithValue("@keyword", keyword);
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
        return result;
    }
}
